package com.hmkj.quote.job;

import com.hmkj.core.constant.CacheID;
import com.hmkj.core.mapper.order.OrderMapper;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.order.OrderService;
import com.hmkj.core.service.risk.RiskParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单处理定时器
 *
 * @author eproo
 * @create 2017-09-20 14:39
 */
@Slf4j
@Component
public class OrderJob {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private RiskParamService riskParamService;

    /**
     * 日内强平定时查询
     */
    @Scheduled(cron = "0 * * * * ?")//每分钟钟查询一次
    public void coverOrderTiming() {
        //获取点买单的订单列表
        List<Commo> commoList = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        commoList.forEach(commo -> {
            // 判断当前时间该品种是否需要进行 日内强平
            if (riskParamService.isAutoCoverTime(commo.getId())) {
                List<Order> list = orderService.select(new Order(o -> {
                    o.setCommoId(commo.getId());
                    o.setStatus(Order.STATUS.T3.code);
                    o.setFollow(Order.FOLLOW.T1.code);
                    o.setDelFlag(Order.DELFLAG.NORMAL.code);
                }));
                list.forEach(os -> {
                    try {
                        Map<String, Object> data = new HashMap<>();
                        data.put("orderId", os.getId());
                        data.put("num", os.getNumber());
                        data.put("settleType", Order.SETTLETYPE.T4.code);
                        redisService.push(CacheID.QUEUE_COVER_ORDER, data);
                    } catch (Exception e) {
                        log.error("订单平仓失败，订单Id:" + os.getId());
                        e.printStackTrace();
                    }

                });
            }
        });
    }

    /**
     * 自动撤单定时器
     */
    @Scheduled(cron = "0 * * * * ?")//每分钟钟查询一次
    public void cancleOrderTiming() {
        //获取点买单的订单列表
        List<Commo> commoList = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        commoList.forEach(commo -> {
            // 判断当前时间该品种是否需要进行 撤单
            if (riskParamService.isAutoCoverTime(commo.getId())) {
                List<Order> list = orderService.select(new Order(o -> {
                    o.setCommoId(commo.getId());
                    o.setStatus(Order.STATUS.T2.code);
                    o.setFollow(Order.FOLLOW.T1.code);
                    o.setDelFlag(Order.DELFLAG.NORMAL.code);
                }));
                list.forEach(os -> {
                    try {
                        orderService.cancelOrder(os.getId(),os.getUserId(),Order.CANCLETYPE.T3);
                    } catch (Exception e) {
                        log.error("订单自动撤单失败，订单Id:" + os.getId());
                        e.printStackTrace();
                    }

                });
            }
        });
    }

    /**
     * 流单定时查询
     */
    @Scheduled(cron="0/1 * * * * ?")// 每一秒钟查询一次
    public void flowOrderTiming() {
        //获取待流单的订单列表
        Map<String, Object> params = new HashMap<>();
        params.put("status", Order.STATUS.T1.code);
        List<Order> orders = orderMapper.selectWaitFlowOrder(params);
        log.info("已查询到待流单订单数：" + orders.size() + "条");
        //放入待流单队列
        orders.forEach(order -> {
            redisService.push(CacheID.QUEUE_FLOW_ORDER, order);
        });
    }
}
